package com.zxq.ai.application.api.impl;

import com.zxq.ai.application.api.IUser;
import com.zxq.ai.application.exception.UserNotFoundException;
import com.zxq.ai.application.repository.CashAccountRepository;
import com.zxq.ai.application.repository.UserRepository;
import com.zxq.ai.domain.enums.UserType;
import com.zxq.ai.domain.service.CashAccountDomain;
import com.zxq.ai.domain.service.UserDomain;
import com.zxq.ai.domain.user.User;
import com.zxq.ai.domain.user.account.CashAccount;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class BUser implements IUser {

    @Resource
    private UserDomain userDomain;
    @Resource
    private CashAccountDomain cashAccountDomain;
    @Resource
    private UserRepository userRepository;
    @Resource
    private CashAccountRepository cashAccountRepository;

    @Override
    public User register(String phoneNo, String userName) {

        User user = userDomain.generateInstance(UserType.B_USER, phoneNo, userName);
        CashAccount cashAccount = cashAccountDomain.generateInstance(user);

        userRepository.save(user);
        cashAccountRepository.save(cashAccount);

        return user;
    }

    @Override
    public User login(String phoneNo) {

        User user = userRepository.queryByPhoneNo(phoneNo);
        if (Objects.isNull(user)) {
            throw new UserNotFoundException(String.format("账号不存在, phoneNo: %s", phoneNo));
        }

        return user;
    }

}
